package com.mhc.haval.controller;


import com.mhc.haval.constant.IsDeletedEnum;
import com.mhc.haval.form.DataDictForm;
import com.mhc.haval.security.model.User;
import com.mhc.haval.vo.DataDictVO;
import com.mhc.suzuki.api.model.DataDictDTO;
import com.mhc.suzuki.api.service.IDataDictService;
import com.subaru.common.entity.BizResult;
import com.subaru.common.entity.ErrorCodeEnum;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * Created by Mr.H
 * on 2017/8/14 15:00.
 */
@Slf4j
@Controller
@RequestMapping(value = "/dataDict")
public class DataDictController {


    @Autowired
    private IDataDictService iDataDictService;

    /**
     * 获取元数据分组列表
     * @param user 登录用户信息
     * @return
     */
    @ApiOperation(value = "获取元数据分组列表", notes = "点击数据维护菜单跳转至元数据管理页面，显示左侧元数据分组列表")
    @RequestMapping(value = "/dataGroupList", method = RequestMethod.GET)
    public @ResponseBody
    BizResult<List<DataDictVO>> getDataGroupList(@AuthenticationPrincipal User user) {

        Optional<User> userOptional = Optional.ofNullable(user);
        if (userOptional.isPresent()){
            //获取公司编号
            String companyNo = user.getCompanyNo();
            log.info(companyNo);
            List<DataDictDTO> dataDictDTOS = iDataDictService.getDataGroupList(companyNo);
            List<DataDictVO> dataDictVOS = copyDOListToVOLIst(dataDictDTOS);
            return BizResult.create(dataDictVOS, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

        } else {
            return BizResult.create(ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrCode(), ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrMsg());
        }

    }


    /**
     * 获取某分组下元数据
     * @param dataGroupCode url变量，分组code
     * @param user 登录用户
     * @return
     */
    @ApiOperation(value = "获取某分组下元数据", notes = "点击分组列表某行，显示该分组下的元数据")
    @ApiImplicitParam(name = "dataGroupCode", value = "分组名称", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/{dataGroupCode}", method = RequestMethod.GET)
    public @ResponseBody
    BizResult<List<DataDictVO>> getGroupData(@PathVariable String dataGroupCode, @AuthenticationPrincipal User user) {
        Optional<User> userOptional = Optional.ofNullable(user);
        if (userOptional.isPresent()){
            //获取公司编号
            String companyNo = user.getCompanyNo();

            List<DataDictDTO> dataDictDTOS = iDataDictService.getGroupDataList(dataGroupCode, companyNo);
            List<DataDictVO> dataDictVOS = copyDOListToVOLIst(dataDictDTOS);
            return BizResult.create(dataDictVOS, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

        } else {
            return BizResult.create(ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrCode(), ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrMsg());
        }

    }


    /**
     * 新增元数据
     * @param dataGroupCode url变量
     * @param dataDictForm 数据详细信息
     * @param user 登录用户信息
     * @return
     */
    @ApiOperation(value = "元数据添加", notes = "添加元数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataDictForm", value = "数据详细信息", paramType = "body", required = true, dataType = "DataDictForm"),
            @ApiImplicitParam(name = "dataGroupCode", value = "分组编码", paramType = "path", required = true, dataType = "String")
    })
    @RequestMapping(value = "/{dataGroupCode}/data", method = RequestMethod.POST)
    public @ResponseBody
    BizResult addDataDict(@PathVariable String dataGroupCode, @RequestBody DataDictForm dataDictForm, @AuthenticationPrincipal User user) {
        Optional<User> userOptional = Optional.ofNullable(user);
        if (userOptional.isPresent()) {
            /*Form->BO*/
            DataDictDTO dataDictDTO = BeanCopierUtil.convert(dataDictForm, DataDictDTO.class);

            //获取公司编号
            String companyNo = user.getCompanyNo();

            dataDictDTO.setCompanyNo(companyNo);
            dataDictDTO.setDataGroup(dataGroupCode);
            dataDictDTO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
            iDataDictService.addDataDict(dataDictDTO);
            return BizResult.create(true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

        } else {
            return BizResult.create(ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrCode(), ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrMsg());
        }
    }


    /**
     * 元数据修改
     * @param dataGroupCode url变量
     * @param dataId url变量
     * @param dataDictForm 数据详细信息
     * @param user 登录用户信息
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "元数据修改", notes = "修改元数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataDictForm", value = "数据详细信息", paramType = "body", required = true, dataType = "DataDictForm"),
            @ApiImplicitParam(name = "dataGroupCode", value = "分组code", paramType = "path", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dataId", value = "元数据编号", paramType = "path", required = true, dataType = "Long")
    })
    @RequestMapping(value = "/{dataGroupCode}/{dataId}", method = RequestMethod.PUT )
    public @ResponseBody
    BizResult updateDataDict(@PathVariable String dataGroupCode, @PathVariable Long dataId, @RequestBody DataDictForm dataDictForm, @AuthenticationPrincipal User user) {
        Optional<User> userOptional = Optional.ofNullable(user);
        if (userOptional.isPresent()) {
            /*Form->BO*/
            DataDictDTO dataDictDTO = BeanCopierUtil.convert(dataDictForm, DataDictDTO.class);

            log.info("登录用户信息：" + user.getCompanyNo());

            //获取公司编号
            String companyNo = user.getCompanyNo();

            dataDictDTO.setDataId(dataId);
            dataDictDTO.setCompanyNo(companyNo);
            dataDictDTO.setDataGroup(dataGroupCode);
            dataDictDTO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
            iDataDictService.updateDict(dataDictDTO);
            return BizResult.create(true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } else {
            return BizResult.create(ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrCode(), ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrMsg());
        }
    }


    /**
     * 删除元数据
     * @param dataGroupCode url变量
     * @param dataId url变量 元数据id
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "元数据删除", notes = "删除元数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataGroupCode", value = "分组名称", paramType = "path", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dataId", value = "元数据编号", paramType = "path", required = true, dataType = "Long")
    })
    @RequestMapping(value = "/{dataGroupCode}/{dataId}", method = RequestMethod.DELETE)
    public @ResponseBody
    BizResult deleteDataDict(@PathVariable String dataGroupCode, @PathVariable Long dataId) {
        DataDictDTO dataDictDTO = new DataDictDTO();
        if(dataId == null){
            return BizResult.create(ErrorCodeEnum.PARAM_ERROR.getErrCode(), ErrorCodeEnum.PARAM_ERROR.getErrMsg());
        }
        dataDictDTO.setDataId(dataId);
        dataDictDTO.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        iDataDictService.deleteDataDict(dataDictDTO);
        return BizResult.create(true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

    }



    /**
     * DataDictDOList转为DataDictVOLIst
     * @param dataDictDTOS
     * @return
     */
    private List<DataDictVO> copyDOListToVOLIst(List<DataDictDTO> dataDictDTOS) {
        List<DataDictVO> dataDictVOS = new ArrayList<>();
        Iterator<DataDictDTO> iterator = dataDictDTOS.iterator();
        while(iterator.hasNext()){
            DataDictDTO dataDictDTO = iterator.next();
            DataDictVO dataDictVO = BeanCopierUtil.convert(dataDictDTO, DataDictVO.class);
            dataDictVOS.add(dataDictVO);
        }
        return dataDictVOS;
    }

}
